package com.catjiu.webeditor.parser;

import com.catjiu.webeditor.ParserContext;
import com.catjiu.webeditor.style.RStyle;
import com.catjiu.webeditor.style.Style;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.jsoup.nodes.TextNode;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;

public class TextNodeParser extends NodeParser{
    @Override
    public void parser(ParserContext context, Style style) {
        TextNode textNode = (TextNode) getNode();
        String txt = textNode.text();
        CTRPr rpr;
        //超链接
        if(context.getRpt()!=null && context.getRpt().equals(ParserContext.RPT.HYPERLINK)){
            CTHyperlink hyperlink = context.getHyperlink();
            CTR ctr = hyperlink.addNewR();
            rpr = ctr.isSetRPr()?ctr.getRPr():ctr.addNewRPr();
            rpr.addNewRStyle().setVal("Hyperlink");
            ctr.addNewT().setStringValue(txt);
        }else{
            XWPFRun run = context.createRun();
            rpr = run.getCTR().isSetRPr()?run.getCTR().getRPr():run.getCTR().addNewRPr();
            run.setText(txt);
        }
        RStyle rStyle = (style instanceof RStyle)? (RStyle) style:new RStyle();
        rStyle.rend(rpr);
    }
}
